package com.jiukuaidao.zhulin.module.commodity.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.library.util.LogUtil;
import com.jiukuaidao.zhulin.api.ApiGetSortProList;
import com.jiukuaidao.zhulin.entity.ApiSortCatList;
import com.jiukuaidao.zhulin.entity.ApiSortPros;
import com.jiukuaidao.zhulin.entity.EventShoppingCartInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.commodity.contract.SortProContract;
import com.jiukuaidao.zhulin.module.home.contract.HomeContract;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : H_Joker
 * Date : 2016/6/6
 * Description :
 */
public class SortProPresenter extends BasePresenter<SortProContract.View, SortProContract.Model> implements SortProContract.Presenter {

    public SortProPresenter(SortProContract.Model repository, SortProContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {

    }

    @Override
    public void setCateInfo(ApiSortCatList.ApiSortCateListItem cateInfo) {
        mRepository.setCateInfo(cateInfo);
    }

    @Override
    public ApiSortCatList.ApiSortCateListItem getCateInfo() {
        return mRepository.getCateInfo();
    }

    @Override
    public void loadProList(final int act, String categoryIds, final int pageIndex, String lat, String lon, String cityCode, String tagId, String brandId, String keyword) {
        mRepository.loadData(new ApiGetSortProList<>(cityCode, lon, lat, categoryIds, pageIndex, tagId, brandId, keyword, new MyRetroCallBack<ApiSortPros>() {
            /**
             * @param result
             * @param errorCode
             * @param msg
             */
            @Override
            public void onSuccess(ApiSortPros result, String errorCode, String msg) {
                if (result != null) {

                    List<ApiSortPros.ApiSortProItem> goodsList = result.goodsList;

                    if (mRepository.getPros() == null) {
                        mRepository.setPros(new ArrayList<ApiSortPros.ApiSortProItem>());
                    }

                    setCurrenPageIndex(pageIndex);

                    if (act == HomeContract.REFRESH) {
                        mRepository.getPros().clear();
                    }
                    if (goodsList != null) {
                        mRepository.getPros().addAll(goodsList);
                    }

                    /**
                     * list 无数据 也需要刷新一下RecyclerView 不然会闪退 是由于vierpager中fragment的生命周期造成的
                     */
//                    if (mRepository.getPros() != null && !mRepository.getPros().isEmpty()) {
                    if (mRepository.getPros() != null ) {
                        // TODO 刷新列表
                        mView.refreshProsUi();

                        if (result.pageIndex >= result.totalPage) {
                            mView.loadComplete(act, true, false);
                        } else {
                            mView.loadComplete(act, false, false);
                        }

                    } else {
                        // TODO 显示空列表界面
                        mView.loadComplete(act, false, true);

                    }
                }
            }

            @Override
            public void onFail(ApiSortPros result, String errorCode, String msg) {
                if (mRepository.getPros() != null && !mRepository.getPros().isEmpty()) {
                    mView.loadComplete(act, false, false);
                } else {
                    mView.loadComplete(act, false, true);
                }
                LogUtil.log(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                if (mRepository.getPros() != null && !mRepository.getPros().isEmpty()) {
                    mView.loadComplete(act, false, false);
                } else {
                    mView.loadComplete(act, false, true);
                }
            }
        }));
    }

    @Override
    public void setCurrenPageIndex(int currenPageIndex) {
        mRepository.setCurrenPageIndex(currenPageIndex);
    }

    @Override
    public int getCurrentPageIndex() {
        return mRepository.getCurrentPageIndex();
    }

    @Override
    public void setPros(List<ApiSortPros.ApiSortProItem> list) {
        mRepository.setPros(list);
    }

    @Override
    public void setShoppingCart(List<EventShoppingCartInfo> datas) {
        mRepository.setShoppingCart(datas);
    }

    @Override
    public List<EventShoppingCartInfo> getShoppingCart() {
        return mRepository.getShoppingCart();
    }

    @Override
    public List<ApiSortPros.ApiSortProItem> getPros() {
        return mRepository.getPros();
    }
}
